<?php
ob_start();


require_once(VFW_ROOT.'includes.php');



makePage(true);

function makePage($start_sess) {
	global $pluginsEnabled;
	$TMOK = false;
	$CTRLOK = false;
	$pluginAct = false;
	$ctrl_instance;
	
	foreach($pluginsEnabled as $plugin) {
		include_once APP_PATH.'plugins_conf/'.$plugin.'.php';
	}
	
	try {

		
		$start = microtime(true);
		if ($start_sess){
			session_start();
		}
		$delorean = new TimeMachine(APP_ID);
		$TMOK = true;
		
		
		$err = '';
		
		list($ctrl, $act) = ActionSelector::getCtrlAct($delorean);
		$delorean->setRequest($_REQUEST);
		if (file_exists(APP_PATH."ctrl/".$ctrl.".php")) {
			require_once(APP_PATH."ctrl/".$ctrl.".php");
		} else {
			if (($file = PluginHandler::search($ctrl))) {
				require_once ($file);
				$pluginAct = true;
			} else {		    
			    foreach ($GLOBALS['callable']['searchCtrl'] as $callable) {
			        if ($callable($ctrl)) break;
			    }   
			}
			$ctrl .= 'Ctrl';
		}
		
		
		
		if (!class_exists($ctrl)) {
		    //$pdio->close();
		    $msg = ($err != ''? $err: "controller \"".APP_PATH."ctrl/".$ctrl.".php"."\" does not exist");
			throw new FWerrorException($msg);
		}
		
		if (!method_exists($ctrl, $act)) {
		    //$pdio->close();
			throw new FWerrorException("method \"".APP_PATH."ctrl/".$ctrl.".php"."::$act\" does not exist");
		}	
		
		
		$pdio = getPdio(DB_TYPE);
		
		$GLOBALS['ServiceHandler']->attachPDO($pdio);
		
		$ctrl_class 	= new ReflectionClass($ctrl);
		$req 			= new Request(isset($_REQUEST) ? $_REQUEST : null);
		$ctrl_instance	= $ctrl_class->newInstanceArgs(array($req, $delorean, $pdio));
		$CTRLOK = true;		
		
		try {
    		foreach ($GLOBALS['callable']['beforeAll'] as $funct) {
    		    $funct();
    		}
    		
    		
    		PluginHandler::inc();
    		PluginHandler::beforeAction($ctrl_instance);
		
		
			$view_mode = $ctrl_instance->invokeAction($act);
		} catch (goBackException $e) {
			$pdio->close();
			makePage(false);
			return;
		} catch (refreshException $re) {
			$pdio->close();
			makePage(false);
			return;
		}  catch (reloadBackException $re) {
			$pdio->close();
			return;
		}
		
		PluginHandler::afterAction($ctrl_instance);
			
		$pdio->close();
		
		$ctrl_instance->getResponse()->set('execTime', microtime(true)-$start);
		$delorean->Record();
		//$ctrl_instance->getResponse()->append('debug', '<br>'.$delorean->toString());
		//$ctrl_instance->getResponse()->append('debug', echoa($GLOBALS['ServiceHandler']->getRouter(),true));
		//$ctrl_instance->getResponse()->append('debug', $_SERVER['HTTP_USER_AGENT']);
		View::show($view_mode, $ctrl_instance->getResponse(), $delorean, clientIsMobile());
		ob_end_flush();
		
		
		
	} catch (FWerrorException $fwe)  {
		
 		if (!isset($_REQUEST['back'])) {
		
 			if ($TMOK && $CTRLOK) {
 			    try {
 				   $ctrl_instance->goBack($fwe->getMessage());
 			    } catch (goBackException $e) {
			       $pdio->close();
			       //ob_clean();
			       makePage(false);
			       return;
 				}   
 			}
 		}
		
		$res = new Response();
		$res->setTpl(VIEW_PATH.'/error.html');
		$res->set('errore', $fwe->getMessage());
		View::showError($res);
	
	} catch (Exception $e) {
		echo $e->getMessage();
		
	}
}




?>
